<?php
class Hour_manager extends Controller {
	function __construct()
	{
		parent::Controller();
		if($this -> session -> userdata('user_id') === FALSE)
			redirect("login/doLogout");
	}
	
	function index($start=0)
	{
		$this -> load -> model('project_model');
		$this -> load -> library('table');
		$data['page_title'] = "Hours and statistics";
		
		$projects = $this -> project_model -> getUsersProjects($this -> session -> userdata('user_id'));
		if(!$start)
		{
			if(date("w") == 1)
				$start = time();
			else
				$start = strtotime("last monday");
		}
		$data['start'] = $start;
		if(!$projects)
			$data['projects_table'] = "<p class=\"small\">User isn't in any project</p>";
		else
		{

			$data['back_link'] = strtotime("-1 week", $start);
			$data['fow_link'] = strtotime("+1 week", $start);
			
			foreach($projects as $project)
			{
				$start_mod = $start;
				$hours = $this -> project_model -> getUserHoursByProject($project['pid'], $this -> session -> userdata('user_id'), $start);
				$tmpl = array(	'table_open' => '<table class="listTable" cellpadding="5" cellspacing="0">',
								'row_start' => '<tr class="even">',
								'row_alt_start' => '<tr class="odd">');
				$this -> table -> set_template($tmpl);
				$tmp[0] = $project['name'];
				$heading[0] = "Project name";
				for($i = 1; $i <= 7; $i++)
				{
					if(isset($hours[$start_mod]))
						$h = $hours[$start_mod];
					else
						$h = 0;
						
					$heading[$i] = date("D Y-m-d", $start_mod);
					$tmp[$i] = "<input type=\"text\" size=\"3\" name=\"hours[{$project['pid']}][$start_mod]\" value=\"$h\" />";
					$start_mod = strtotime("+1 day", $start_mod);
				}
				$this -> table -> add_row($tmp);
				$this -> table -> set_heading($heading);
			}
			$data['projects_table'] = $this -> table -> generate();
		}
		
		$this -> load -> view('hours_view', $data);
	}
	
	function manageHours($start)
	{
		$this -> load -> model('project_model');
		
		foreach($this -> input -> post('hours') as $pid => $project)
		{
			foreach($project as $time => $amount)
			{
				$this -> project_model -> manageHours($this -> session -> userdata('user_id'), $pid, $time, $amount);		
			}
		}
		
		redirect("hour_manager/index/$start");
	}
	
	/**
	 * This function just loads the data (hours per user) and displays it
	 * Image is generated externally
	 *
	 * @param unknown_type $type
	 */
	function listHours($type=0)
	{
		if($this -> input -> post("start"))
			$start = strtotime($this -> input -> post("start"));
		else
			$start = strtotime("-1 month");
		if($this -> input -> post("end"))
			$end = strtotime($this -> input -> post("end"));
		else
			$end = time();
			
		$data['page_title'] = "Hour Reports - text based";
		$data['start']	= $start;
		$data['end'] 	= $end;
		
		$this -> load -> library('table');
		$this -> load -> model('project_model');
		
		switch($type)
		{
			case 1:
				$hours = $this -> project_model -> getSumHoursPerProject($start, $end);
				break;
			default:
				$hours = $this -> project_model -> getSumHoursPerUser($start, $end);
		}
		
		$data['type'] = (int)$type;
		if(empty($hours))
			$data['statistics_table'] = "<p class=\"small\">No hours within selected timeslot</p>";
		else
		{
			$tmpl = array(	'table_open' => '<table class="listTable" cellpadding="5" cellspacing="0">',
							'row_start' => '<tr class="even">',
							'row_alt_start' => '<tr class="odd">');
			$this -> table -> set_template($tmpl);
			$this -> table -> set_heading("Name", "Amount of hours");
			$data['statistics_table'] = $this -> table -> generate($hours);
		}
		$this -> load -> view("report", $data);
	}
	
	/**
	 * Prints the image
	 *
	 * @param unknown_type $start
	 * @param unknown_type $end
	 */
	function listHoursGraph($start, $end, $type)
	{
		include (APPPATH ."external/jpgraph.php");
		include (APPPATH ."external/jpgraph_pie.php");
		include (APPPATH ."external/jpgraph_pie3d.php");
		include (APPPATH ."external/jpgraph_bar.php");
		
		$graph = new PieGraph(400,400,"auto");
		
		$this -> load -> model('project_model');
		
		switch($type)
		{
//			case 2:
//				$hours = $this -> project_model -> getSumHoursPerProject($start, $end);
//				foreach($hours as $item)
//				{
//					$amount[] = $item['amount'];
//					$legend[] = $item['name'];
//				}
//				$graph->SetScale("textlin");
//				$graph->yaxis->scale->SetGrace(20);
//				
//				// Add a drop shadow
//				$graph->SetShadow();
//				
//				// Adjust the margin a bit to make more room for titles
//				$graph->img->SetMargin(40,30,20,40);
//				$graph->xaxis->SetTickLabels($legend);
//				
//				// Create a bar pot
//				$p1 = new BarPlot($amount);
//				
//				// Adjust fill color
//				$p1->SetFillColor('orange');
//				$p1->SetShadow();
//				$p1->value->Show();
//				$p1->value->SetFont(FF_ARIAL,FS_BOLD,10);
//				$p1->value->SetAngle(45);
//				$p1->value->SetFormat('%0.1f');
//				break;
			case 1:
				$hours = $this -> project_model -> getSumHoursPerProject($start, $end);
				foreach($hours as $item)
				{
					$amount[] = $item['amount'];
					$legend[] = $item['name'];
				}
				$graph->title->Set("Total hours per project");
				$p1 = new PiePlot3D($amount);
				$p1->SetSize(0.5);
				$p1->SetCenter(0.45);
				$p1->SetLegends($legend);
				$p1->SetShadow();
				$p1->ExplodeAll();
				break;
			default:
				$hours = $this -> project_model -> getSumHoursPerUser($start, $end);
				foreach($hours as $item)
				{
					$amount[] = $item['amount'];
					$legend[] = $item['name'];
				}
				$graph->title->Set("Total hours per user");
				$p1 = new PiePlot3D($amount);
				$p1->SetSize(0.5);
				$p1->SetCenter(0.45);
				$p1->SetLegends($legend);
				$p1->SetShadow();
				$p1->ExplodeAll();
		}
			
		$graph->SetShadow();
		$graph->title->SetFont(FF_FONT1,FS_BOLD);
		$graph->Add($p1);
		$graph->Stroke();
	}
}